import { _decorator, BoxCollider2D, Collider2D, Component, Node, RigidBody2D, Vec2, ERigidBody2DType, CircleCollider2D } from 'cc';
import { Player } from './Player';
const { ccclass, property } = _decorator;

@ccclass('Sword')
export class Sword extends Component {
    private rb: RigidBody2D | null = null;


    protected onLoad(): void {
        // 获取组件
        this.rb = this.node.getComponent(RigidBody2D);
    }

    update(deltaTime: number) {
        const velocity = this.rb.linearVelocity;
        if (!velocity.equals(Vec2.ZERO)) {
            const angle = Math.atan2(velocity.y, velocity.x) * 180 / Math.PI;
            this.node.angle = angle - 90;
        }
    }

    public setUpSword(_dir: Vec2, _gravityScale: number, _player: Player, _freezeTimeDuration: number) {
        const angle = Math.atan2(_dir.y, _dir.x) * 180 / Math.PI;
        this.node.angle = angle - 90;
        this.rb.gravityScale = _gravityScale;
        this.rb.linearVelocity = _dir;
    }
}


